#### Generate Figure: Income by Agreement ####

#### Descriptives ####
fig4_dat = bind_rows(import(here("data","fig4_inc_agree_dat.rds")) |> 
                       mutate(agree = "Agree"),
                     import(here("data","fig4_inc_disagree_dat.rds")) |> 
                       mutate(agree = "Disagree")) |> 
  invert_group() |> fix_healthcare()

## N significant
ccat("Number of (in)significant differences (income groups):")
fig4_dat |> 
  group_by(is_sig_int) |> 
  count() |> 
  mutate(n = n/2) # Account for double-counting of policies
  
## Average difference
ccat("Average significant difference:")
fig4_dat |> 
  filter(is_sig_int == 1) |> 
  pivot_wider(id_cols = c("condition","agree"),
              names_from = group, values_from = effect,
              names_prefix = "group_") |> 
  mutate(abs_diff = abs(group_0 - group_1)) |> 
  summarise(avg_abs_diff = mean(abs_diff))

## Correlation
ccat("High/low income correlation:")
fig4_dat |> 
  pivot_wider(id_cols = c("condition","agree"),
              names_from = group, values_from = effect,
              names_prefix = "group_") |> 
  summarise(cor = cor(group_0, group_1))

#### Plot ####
fig_inc_agree = plot_conjoint(conjoint = fig4_dat |> 
                             filter(agree == "Agree"),
                           is_result = T,
                           lab = policies, 
                           weights = dta_grp$demog_weight[!is.na(dta_grp$inc_grp)],
                           use="for",
                           title=NULL,
                           grouped_data=T) +
  labs(subtitle = "Agree with Policy", x = NULL, y = NULL)

fig_inc_disagree = plot_conjoint(conjoint = fig4_dat |> 
                                filter(agree == "Disagree"),
                              is_result = T,
                              lab = policies, 
                              weights = dta_grp$demog_weight[!is.na(dta_grp$inc_grp)],
                              use="against",
                              title=NULL,
                              grouped_data=T) +
  labs(subtitle = "Disagree with Policy", x = NULL, y = NULL)

fig_inc_pts = fig_inc_agree + fig_inc_disagree

print(fig_inc_pts)

ggsave(here("figures","figure_4.png"), fig_inc_pts,
       dpi = 600, units = "in", width = 10, height = 9)

ccat("See 'figure_4.png' for Figure 4 output")
